package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.opportunityuser.pojo.dto.OpportunityUserDeleteBatchDTO;
import com.xbongbong.pro.opportunityuser.pojo.vo.OpportunityUserDeleteBatchVO;
import com.xbongbong.saas.domain.entity.OpportunityUserEntity;
import com.xbongbong.saas.model.OpportunityUserModel;
import com.xbongbong.saas.service.OpportunityUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/3/10 16:18
 * @since v1.0
 */
@Service("opportunityUserService")
public class OpportunityUserServiceImpl implements OpportunityUserService {

    private static final Logger LOG = LoggerFactory.getLogger(OpportunityUserServiceImpl.class);

    @Resource
    private OpportunityUserModel opportunityUserModel;

    @Override
    public OpportunityUserDeleteBatchVO deleteBatch(OpportunityUserDeleteBatchDTO opportunityUserDeleteBatchDTO) throws XbbException {
        OpportunityUserDeleteBatchVO opportunityUserDeleteBatchVO = new OpportunityUserDeleteBatchVO();
        try {
            List<Long> opportunityIdIn = opportunityUserDeleteBatchDTO.getOpportunityIdIn();
            String corpid = opportunityUserDeleteBatchDTO.getCorpid();
            opportunityUserModel.deleteBatchByOpportunityIdIn(opportunityIdIn, corpid);
        } catch (Exception e) {
            LOG.error("ContactUserServiceImpl.deteleBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return opportunityUserDeleteBatchVO;
    }

    @Override
    public void dataConsistencyUpdateUser(String corpid, String userId, String userName, String userAvatar) throws XbbException {
        try {
            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("userId", userId);
            param.put("del", 0);
            List<OpportunityUserEntity> opportunityUserEntityList = opportunityUserModel.findEntitys(param);
            for (OpportunityUserEntity entity : opportunityUserEntityList) {
                entity.setUserName(userName);
                entity.setUserAvatar(userAvatar);
                opportunityUserModel.update(entity);
            }
        } catch (Exception e) {
            LOG.error("OpportunityUserServiceImpl.dataConsistencyUpdateUser 出错，corpid=" + corpid + " userId=" + userId + "  userName=" + userName + "  userAvatar=" + userAvatar, e);
        }
    }
}
